#include<queue>
#include<string>
#include<list>
#include<iostream>
using std::list;
using std::string;
using std::to_string;
using std::stoi;



struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};



class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        if (!root) return "null";
        else return to_string(root->val) + "," + serialize(root->left) + "," + serialize(root->right);
    }


    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        list<string> dataArray = {};
        string str;
        for (auto& ch: data) {
            if (ch != ',') str.push_back(ch);
            else {
                dataArray.push_back(str);
                str.clear();
            }
        }
        if (!str.empty()) {
            dataArray.push_back(str);
            str.clear();
        }
        for (auto& str: dataArray) {
            std::cout << str << " ";
        }
        std::cout << "\n";
        TreeNode* root = rdeserialize(dataArray);
        return root;
    }

    TreeNode* rdeserialize(list<string>& dataArray) {
        if (dataArray.empty()) return nullptr;
        string str = dataArray.front();
        
        if (str == "null") {
            dataArray.erase(dataArray.begin());
            return nullptr;
        }

        TreeNode* root = new TreeNode(stoi(dataArray.front()));
        dataArray.erase(dataArray.begin());
        root->left = rdeserialize(dataArray);
        root->right = rdeserialize(dataArray);
        return root;

    }

};
